package com.hxd.otc.module.convenience.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.convenience.adapter.HydropwerLabelAdapter;
import com.hxd.otc.module.convenience.adapter.HydropwerListAdapter;
import com.hxd.otc.module.convenience.bean.ConvenienveHistoryBean;
import com.hxd.otc.module.convenience.bean.NorGoodsBean;
import com.hxd.otc.module.convenience.bean.RefuelingServiceBean;
import com.hxd.otc.module.convenience.bean.WaterCoalBillBean;
import com.hxd.otc.module.convenience.contract.HydropwerListContract;
import com.hxd.otc.module.convenience.fragment.ConvenienceFragment;
import com.hxd.otc.module.convenience.presenter.HydropowerListPresenter;
import com.hxd.otc.widget.SwipeItemLayout;
import com.hxd.otc.widget.TitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HydropowerListActivity extends BaseActivity<HydropowerListPresenter> implements HydropwerListContract.View {

    @BindView(R.id.title_bar)
    TitleBar titleBar;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @BindView(R.id.rc_label)
    RecyclerView rcLabel;

    @BindView(R.id.rc_hydropower)
    RecyclerView rcHydropower;

    @BindView(R.id.tv_more_records)
    TextView tvMoreRecords;

    HydropwerListAdapter hydropwerListAdapter;
    HydropwerLabelAdapter hydropwerLabelAdapter;
    RefuelingServiceBean serviceBean;
    boolean isShowMore = true;
    int showSize = 3;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_hydropower_list;
    }

    @Override
    protected void initView() {
        serviceBean = (RefuelingServiceBean) getIntent().getExtras().getSerializable(ConvenienceFragment.EXTRA_REFUELING_SERVICE_BEAN);
        rcLabel.setLayoutManager(new LinearLayoutManager(this));
        rcHydropower.setLayoutManager(new LinearLayoutManager(this));
        rcLabel.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(this));
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.onLoadLabel(serviceBean.businessType);
                mPresenter.onLoadHydropwer(serviceBean.getId());
            }
        });
    }

    private void initHydropwerAdapter() {
        if (hydropwerListAdapter == null) {
            hydropwerListAdapter = new HydropwerListAdapter();
            hydropwerListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(HydropwerDetailActivity.NORGOODSBEAN, hydropwerListAdapter.getItem(position));
                    $startActivity(ChooseHydropowerCityActivity.class, bundle);
                }
            });
        }
        rcHydropower.setAdapter(hydropwerListAdapter);
    }

    private void initLabelAdapter() {
        if (hydropwerLabelAdapter == null) {
            hydropwerLabelAdapter = new HydropwerLabelAdapter();
            hydropwerLabelAdapter.setCallBack(new HydropwerLabelAdapter.LabelCallBack() {
                @Override
                public void delete(String id) {
                    mPresenter.onDelete(id);
                }

                @Override
                public void onClickLabel(ConvenienveHistoryBean item) {
                    historyBean = item;
                    mPresenter.getArrearage(item.rechargeAccount, item.itemId);
                }
            });
        }
        rcLabel.setAdapter(hydropwerLabelAdapter);
    }

    ConvenienveHistoryBean historyBean;

    @Override
    protected void initData() {
        initLabelAdapter();
        initHydropwerAdapter();
        mRefreshLayout.autoRefresh(0, 10, 1);
    }

    @OnClick({R.id.tv_more_records})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_more_records:
                if (isShowMore) {
                    isShowMore = false;
                    hydropwerLabelAdapter.setNewData(mPresenter.getList());
                    hydropwerLabelAdapter.notifyDataSetChanged();
                    tvMoreRecords.setText(getString(R.string.collapse));
                } else {
                    isShowMore = true;
                    tvMoreRecords.setText(getString(R.string.more_records));
                    if (mPresenter.getList() != null && mPresenter.getList().size() >= showSize) {
                        hydropwerLabelAdapter.setNewData(mPresenter.getList().subList(0, showSize));
                    } else {
                        hydropwerLabelAdapter.setNewData(mPresenter.getList());
                    }
                    hydropwerLabelAdapter.notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    protected void initEvent() {
    }

    @Override
    protected HydropowerListPresenter createPresenter() {
        return new HydropowerListPresenter(this);
    }

    @Override
    public void onSetHydropwerAdapter(List<NorGoodsBean> list) {
        hydropwerListAdapter.setNewData(list);
    }

    @Override
    public void onLabelShowNoMore() {
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void onSetLabelAdapter(List<ConvenienveHistoryBean> list) {
        if (list == null || list.size() <= 0) {
            tvMoreRecords.setVisibility(View.GONE);
        } else {
            tvMoreRecords.setVisibility(View.VISIBLE);
            rcLabel.setVisibility(View.VISIBLE);
            if (list.size() >= showSize) {
                hydropwerLabelAdapter.setNewData(list.subList(0, showSize));
            } else {
                hydropwerLabelAdapter.setNewData(list);
            }
        }
    }

    @Override
    public void onHydropwerShowNoMore() {
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void onDeleteSucced() {
        showToast(getString(R.string.delete_succeed));
        rcLabel.setVisibility(View.GONE);
        mPresenter.onLoadLabel(serviceBean.businessType);
    }

    @Override
    public void setArrearage(WaterCoalBillBean bean) {
        Bundle bundle = new Bundle();
        if (historyBean.flagType == 1) {//水费
            for (int i = 0; i < hydropwerListAdapter.getData().size(); i++) {
                if (hydropwerListAdapter.getData().get(i).getModuleCode().equals("c2670")) {
                    bundle.putSerializable(HydropwerDetailActivity.NORGOODSBEAN, hydropwerListAdapter.getData().get(i));
                    break;
                }
            }
        } else {//电费
            for (int i = 0; i < hydropwerListAdapter.getData().size(); i++) {
                if (hydropwerListAdapter.getData().get(i).getModuleCode().equals("c2680")) {
                    bundle.putSerializable(HydropwerDetailActivity.NORGOODSBEAN, hydropwerListAdapter.getData().get(i));
                    break;
                }
            }
        }

        bundle.putSerializable("ConvenienveHistoryBean", historyBean);
        bundle.putSerializable("billBean", bean);
        bean.customerAddress = historyBean.rechargeAccount;
        bean.accountNo = historyBean.rechargeAccount;
        $startActivity(HydropwerDetailActivity.class, bundle);
    }


}
